<template>
  <div>
    <van-nav-bar title="注冊" />
    <van-form @submit="onSubmit" autocomplete="off">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^[\w-]{4,16}$/, message: '请输入4-6位数' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^[\w-]{8,16}$/, message: '请填写8-16位密码' },
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="primary" native-type="submit">注册</van-button>
      </div>
      <router-link class="link" to="/login"> 已有账号，去登陆 </router-link>
    </van-form>
  </div>
</template>

<script>
import { register } from '../api/users'
export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      this.$toast.loading({
        // 发请求后请求提示
        message: '疯狂加载中...',
        forbidClick: true
        // duration: 0
      })
      await register(values)
      this.$toast('注册成功')
      this.$router.push('/login')
    }
  }
}
</script>

<style>
  .link{
    color: #07c160;
    font-size: 12px;
    float: right;
    padding-right: 20px;
  }
</style>
